package net.mushystuff.dogalogue.services.endpoints;

import java.rmi.RemoteException;
import java.util.Set;

import javax.xml.rpc.ServiceException;

import net.mushystuff.dogalogue.beans.Breed;
import net.mushystuff.dogalogue.beans.Dog;
import net.mushystuff.dogalogue.services.pojo.DogServiceImpl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;

public class DogServiceEndpoint extends ServletEndpointSupport {

	@SuppressWarnings("unused")
	private Log log = LogFactory.getLog(this.getClass());

	private static DogServiceImpl dogService;

	public DogServiceEndpoint() {
		super();
	}

	@Override
	protected void onInit() throws ServiceException {
		if (dogService == null) {
			dogService = (DogServiceImpl) getWebApplicationContext().getBean(
					"dogService");
		}
	}

	public Breed[] getBreeds() throws RemoteException {
		Set<Breed> breeds = dogService.getAllBreeds();
		Breed[] result = new Breed[breeds.size()];
		result = breeds.toArray(result);
		return result;
	}

	public Dog getDog(String uuid) throws RemoteException {
		return dogService.getDog(uuid);
	}
	
	public void addDog(String name, Breed breed) throws RemoteException {
		dogService.addDog(new Dog(name, breed));
	}

}
